#include "adminmainwindow.h"
#include <QVBoxLayout>
#include <QHBoxLayout>
#include <QPushButton>
#include <QLabel>
#include <QMenuBar>
#include <QToolBar>
#include <QStatusBar>
#include <QMessageBox>
#include <QStackedWidget>

AdminMainWindow::AdminMainWindow(QWidget *parent) : QMainWindow(parent),
    m_adminManager(nullptr)
{
    // 设置窗口图标
    setWindowIcon(QIcon(":/images/icon.svg"));

    // 创建管理员管理器
    m_adminManager = new AdminManager(this);
    setupUI();
    setupMenus();
    setupToolbars();
    setupConnections();

    // 初始显示添加员工界面
    //onAddEmployee();
}

AdminMainWindow::~AdminMainWindow()
{
    delete addEmployeeView;
    delete selectEmployeeView;
    delete departmentView;
    delete assessmentView;
    delete salaryView;
    delete attendanceView;
    delete leaveRecordView;
    delete m_adminManager;
}

void AdminMainWindow::setupUI()
{
    setWindowTitle("管理员系统");
    setMinimumSize(1000, 800);

    // 创建中央部件和堆栈窗口
    QWidget *centralWidget = new QWidget(this);
    QVBoxLayout *mainLayout = new QVBoxLayout(centralWidget);

    // 创建标题栏
    QHBoxLayout *titleLayout = new QHBoxLayout();
    QLabel *titleLabel = new QLabel("员工管理系统 - 管理员界面");
    titleLabel->setObjectName("adminTitleLabel");
    QFont titleFont("Microsoft YaHei", 16, QFont::Bold);
    titleLabel->setFont(titleFont);
    titleLabel->setAlignment(Qt::AlignCenter);
    titleLayout->addWidget(titleLabel);

    // 创建堆栈窗口用于切换不同视图
    QStackedWidget *stackedWidget = new QStackedWidget(this);

    // 初始化各个视图
    addEmployeeView = new AdminAddEmployeeView(this);
    selectEmployeeView = new AdminSelectEmployeeView(this);
    leaveEmployeeView = new AdminLeaveEmployeeView(this);
    departmentView = new DepartmentView(this);
    assessmentView = new MonthAssessmentView(this);
    salaryView = new MonthSalaryView(this);
    attendanceView = new DayCheckView(this);
    departmentChangeView = new DepartmentChangeView(this);
    // 确保m_adminManager已初始化
    leaveRecordView = new LeaveRecordView(m_adminManager, this);

    // 添加视图到堆栈窗口
    stackedWidget->addWidget(addEmployeeView);
    stackedWidget->addWidget(selectEmployeeView);
    stackedWidget->addWidget(leaveEmployeeView);
    stackedWidget->addWidget(departmentView);
    stackedWidget->addWidget(assessmentView);
    stackedWidget->addWidget(salaryView);
    stackedWidget->addWidget(attendanceView);
    stackedWidget->addWidget(departmentChangeView);
    stackedWidget->addWidget(leaveRecordView);

    // 将标题栏和堆栈窗口添加到主布局
    mainLayout->addLayout(titleLayout);
    mainLayout->addWidget(stackedWidget);

    setCentralWidget(centralWidget);
    statusBar()->showMessage("就绪");
}

void AdminMainWindow::setupMenus()
{
    // 创建菜单栏
    QMenuBar *menuBar = this->menuBar();

    // 文件菜单
    QMenu *fileMenu = menuBar->addMenu("文件");
    fileMenu->addAction("退出", this, &AdminMainWindow::close);

    // 员工管理菜单
    QMenu *employeeMenu = menuBar->addMenu("员工管理");
    employeeMenu->addAction("添加员工", this, &AdminMainWindow::onAddEmployee);
    employeeMenu->addAction("查询员工", this, &AdminMainWindow::onSelectEmployee);
    employeeMenu->addAction("员工离职", this, &AdminMainWindow::onLeaveEmployee);
    // 部门管理菜单
    QMenu *departmentMenu = menuBar->addMenu("部门管理");
    departmentMenu->addAction("部门信息", this, &AdminMainWindow::onDepartmentManagement);
    departmentMenu->addAction("部门调换审核", this, &AdminMainWindow::onDepartmentChangeManagement);

    // 考核管理菜单
    QMenu *assessmentMenu = menuBar->addMenu("考核管理");
    assessmentMenu->addAction("月度考核", this, &AdminMainWindow::onAssessmentManagement);

    // 请假管理菜单
    QMenu *leaveMenu = menuBar->addMenu("请假管理");
    leaveMenu->addAction("请假审核", this, &AdminMainWindow::onLeaveRecordManagement);

    // 薪资管理菜单
    QMenu *salaryMenu = menuBar->addMenu("薪资管理");
    salaryMenu->addAction("月度薪资", this, &AdminMainWindow::onSalaryManagement);

    // 考勤管理菜单
    QMenu *attendanceMenu = menuBar->addMenu("考勤管理");
    attendanceMenu->addAction("打卡详情", this, &AdminMainWindow::onAttendanceManagement);

    // 帮助菜单
    QMenu *helpMenu = menuBar->addMenu("帮助");
    helpMenu->addAction("关于", [this]() {
        QMessageBox::about(this, "关于", "员工管理系统 v1.0\n\n管理员界面");
    });
}

void AdminMainWindow::setupToolbars()
{
    // 创建工具栏
    QToolBar *toolBar = addToolBar(tr("管理员操作"));
    toolBar->setMovable(false);

    // 添加工具按钮
    toolBar->addAction(QIcon(":/images/add_employee.svg"), "添加员工", this, &AdminMainWindow::onAddEmployee);
    toolBar->addSeparator();
    toolBar->addAction(QIcon(":/images/select_employee.svg"), "查询员工", this, &AdminMainWindow::onSelectEmployee);
    toolBar->addSeparator();
    toolBar->addAction(QIcon(":/images/leave_employee.svg"), "员工离职", this, &AdminMainWindow::onLeaveEmployee);
    toolBar->addSeparator();
    toolBar->addAction(QIcon(":/images/department.svg"), "部门管理", this, &AdminMainWindow::onDepartmentManagement);
    toolBar->addSeparator();
    toolBar->addAction(QIcon(":/images/assessment.svg"), "考核管理", this, &AdminMainWindow::onAssessmentManagement);
    toolBar->addSeparator();
    toolBar->addAction(QIcon(":/images/salary.svg"), "薪资管理", this, &AdminMainWindow::onSalaryManagement);
    toolBar->addSeparator();
    toolBar->addAction(QIcon(":/images/attendance.svg"), "打卡详情", this, &AdminMainWindow::onAttendanceManagement);
    toolBar->addSeparator();
    toolBar->addAction(QIcon(":/images/department_change.svg"), "部门调换审核", this, &AdminMainWindow::onDepartmentChangeManagement);
    toolBar->addSeparator();
    toolBar->addAction(QIcon(":/images/leave.svg"), "请假审核", this, &AdminMainWindow::onLeaveRecordManagement);
    toolBar->addSeparator();
    toolBar->addAction(QIcon(":/images/logout.svg"), "退出登录", this, &AdminMainWindow::onLogout);
}

void AdminMainWindow::setupConnections()
{
    // 连接部门视图的返回信号
    connect(departmentView, &DepartmentView::backToMain, this, &AdminMainWindow::onBackToMain);
}

void AdminMainWindow::setAdminManager(AdminManager *manager)
{
    if (manager)
    {
        if (attendanceView)
        {
            attendanceView->setAdminManager(manager);
        }
        if (departmentChangeView)
        {
            departmentChangeView->setAdminManager(manager);
        }
        if (assessmentView)
        {
            assessmentView->setAdminManager(manager);
        }
    }
}

void AdminMainWindow::onAddEmployee()
{
    // 切换到添加员工视图
    addEmployeeView->loadDepartments();
    QStackedWidget *stackedWidget = qobject_cast<QStackedWidget*>(centralWidget()->layout()->itemAt(1)->widget());
    stackedWidget->setCurrentWidget(addEmployeeView);
    statusBar()->showMessage("添加员工");
}

void AdminMainWindow::onSelectEmployee()
{
    // 切换到查询员工视图
    QStackedWidget *stackedWidget = qobject_cast<QStackedWidget*>(centralWidget()->layout()->itemAt(1)->widget());
    stackedWidget->setCurrentWidget(selectEmployeeView);
    statusBar()->showMessage("查询员工");
}

void AdminMainWindow::onDepartmentManagement()
{
    // 切换到部门管理视图
    QStackedWidget *stackedWidget = qobject_cast<QStackedWidget*>(centralWidget()->layout()->itemAt(1)->widget());
    stackedWidget->setCurrentWidget(departmentView);
    // 设置管理员管理器
    departmentView->setAdminManager(m_adminManager);
    statusBar()->showMessage("部门管理");
}

void AdminMainWindow::onAssessmentManagement()
{
    // 切换到考核管理视图
    assessmentView->refreshDepartmentList();
    QStackedWidget *stackedWidget = qobject_cast<QStackedWidget*>(centralWidget()->layout()->itemAt(1)->widget());
    stackedWidget->setCurrentWidget(assessmentView);
    statusBar()->showMessage("考核管理");
}

void AdminMainWindow::onSalaryManagement()
{
    // 切换到薪资管理视图
    salaryView->refreshDepartmentList();
    QStackedWidget *stackedWidget = qobject_cast<QStackedWidget*>(centralWidget()->layout()->itemAt(1)->widget());
    stackedWidget->setCurrentWidget(salaryView);
    statusBar()->showMessage("薪资管理");
}

void AdminMainWindow::onLeaveEmployee()
{
    // 切换到员工离职视图
    QStackedWidget *stackedWidget = qobject_cast<QStackedWidget*>(centralWidget()->layout()->itemAt(1)->widget());
    stackedWidget->setCurrentWidget(leaveEmployeeView);
    statusBar()->showMessage("员工离职");
}

void AdminMainWindow::onAttendanceManagement()
{
    // 切换到打卡详情视图
    QStackedWidget *stackedWidget = qobject_cast<QStackedWidget*>(centralWidget()->layout()->itemAt(1)->widget());
    stackedWidget->setCurrentWidget(attendanceView);
    statusBar()->showMessage("打卡详情");
}

void AdminMainWindow::onDepartmentChangeManagement()
{
    QStackedWidget *stackedWidget = qobject_cast<QStackedWidget*>(centralWidget()->layout()->itemAt(1)->widget());
    stackedWidget->setCurrentWidget(departmentChangeView);
    statusBar()->showMessage("部门调换审核");
}

void AdminMainWindow::onLeaveRecordManagement()
{
    QStackedWidget *stackedWidget = qobject_cast<QStackedWidget*>(centralWidget()->layout()->itemAt(1)->widget());
    stackedWidget->setCurrentWidget(leaveRecordView);
    statusBar()->showMessage("请假记录审核");
}

void AdminMainWindow::onLogout()
{
    // 退出登录，关闭当前窗口
    close();
}

void AdminMainWindow::onBackToMain()
{
    // 返回主界面，这里默认切换到添加员工视图
    onAddEmployee();
}
